const formidable = require('formidable')
const path = require('path');
const {Article} = require('../../model/article');

module.exports = async (req, res) => {
  // 创建表单解析对象
  const form = new formidable.IncomingForm();
  // 上传到服务器中的路径
  form.uploadDir = path.join(__dirname, '../', '../', 'public', 'uploads');
  // 保留上传文件的后缀
  form.keepExtensions = true;

  form.parse(req, async(err, fields, files) => {
    // fields是普通字段信息，files是文件字段信息
    await Article.create({
      title: fields.title,
      author: fields.author,
      publishDate: fields.publishDate,
      cover: files.cover.path.split('public')[1],
      content: fields.content,
    });
    res.redirect('/admin/article')
  })
}